(function($, doc) {
	$.plusReady(function() {
		mui.init();
		var user = app.getState();
		var uid = user.user_id;
		var postpass = md5(user.password);
		var self = plus.webview.currentWebview();
		var token = self.token;
		var price = self.price;
		//console.log(token);
		var coindata = ethscan.getCoindata(token);
		var ctype = coindata['ctype'];
		var myWallet = ethscan.getWallet(ctype);
		var address = myWallet.address;
		if(!address) {
			$.alert("请创建钱包！");
			plus.webview.currentWebview().close();
		}
		var pricelist = ethscan.getpriceList();
		var coinname = coindata['name'];
		var currentpirce = pricelist[coinname];
		var sysprice = pricelist['GATC'];
		
		//console.log(address);
		doc.getElementById('address').value = address;
		doc.getElementById('coinname').innerHTML = coinname;
		doc.getElementById('coinname1').innerHTML = coinname;
		
		doc.getElementById('gatcprice').innerHTML = '$' + sysprice;
		doc.getElementById('currentname').innerHTML = coinname;
		doc.getElementById('currentprice').innerHTML = '$' + currentpirce;
		function toDecimal(x) {
			var f = parseFloat(x);
			if(isNaN(f)) {
				return;
			}
			f = Math.round(x * 100) / 100;
			return f;
		}
		doc.getElementById('count').addEventListener('input', function(){
			var val = this.value;
			if(!val || val == '' || isNaN(val)){
				doc.getElementById('showdesc').innerHTML = '';
				return;
			}
			//console.log(val);
			var num = val*currentpirce/sysprice;
			//console.log(num);
			doc.getElementById('showdesc').innerHTML = toDecimal(num);
			doc.getElementById('money').value = toDecimal(num);
		});
		ethscan.getAmount(coindata, function(money){
			doc.getElementById('hascount').value = money;
		});
		doc.getElementById('btn').addEventListener('tap', function(){
			var hascount = doc.getElementById("hascount").value*1;
			var count = doc.getElementById('count').value*1;
			var money = doc.getElementById('money').value;
			var passwd = doc.getElementById('passwd').value;
			//console.log(hascount);
			//console.log(money);
			var showdesc = doc.getElementById('showdesc').innerHTML*1;
			
			
			
			if(!money || isNaN(money) || money <= 0) {
				$.alert('想要获得的数量必须为大于0的数字');
				doc.getElementById('money').value = '';
				doc.getElementById('money').focus();
				return;
			}
			if(!count || isNaN(count) || count <= 0) {
				$.alert('购买的数量必须为大于0的数字');
				doc.getElementById('count').value = '';
				doc.getElementById('count').focus();
				return;
			}
			if(hascount < count) {
				$.alert('出售的数量不能大于现有的数量');
				//doc.getElementById('count').value = hascount;
				doc.getElementById('count').value = '';
				doc.getElementById('count').focus();
				return;
			}
			if(money < showdesc*0.9) {
				$.alert('想要获得的数量不能少于' + showdesc*0.9);
				doc.getElementById('money').value = '';
				doc.getElementById('money').focus();
				return;
			}
			if(money > showdesc*1.1) {
				$.alert('想要获得的数量不能大于' + showdesc*1.1);
				doc.getElementById('money').value = '';
				doc.getElementById('money').focus();
				return;
			}
			if(!passwd) {
				$.alert('交易密码不能为空');
				return;
			}
			
			var postdata = {
				u : uid,
				p1 : passwd,
				btype : 2,
				ctype : token,
				c : count,
				money : money,
				wallet : address
			};
			console.log(JSON.stringify(postdata));
			$.post(weburl + 'Home/ChatApi/createTrade', postdata, function(res){
				$.alert(res.msg);
				if(res && res.status > 0) {
					var marketWin = plus.webview.getWebviewById('market.html');
					mui.fire(marketWin, 'refreshList', null);
					plus.webview.currentWebview().close();
				}
			});
		},false);
		//doc.getElementById('seeprice').innerHTML = '$' + price;
		//var defprice = ethscan.getDefprice();
		
		//console.log(JSON.stringify(coindata));
		
	});
}(mui, document));